package com.zr.action;

import java.util.Map;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;

import org.apache.struts2.interceptor.ServletResponseAware;
import org.apache.struts2.interceptor.SessionAware;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;

import com.opensymphony.xwork2.ActionSupport;
import com.zr.model.LMdistributionPerson;
import com.zr.model.LMuser;
import com.zr.service.NewlyIincreasedStaffService;
import com.zr.util.PrintWriterUtil;

import net.sf.json.JSONObject;

@Controller
@Scope(value="prototype")
public class NewlyIincreasedStaffAction extends ActionSupport implements SessionAware,ServletResponseAware{
	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;

	@Resource
	private NewlyIincreasedStaffService NStaffservice;
	
	private Map<String, Object> session;
	private HttpServletResponse response;
	
	private int page;
	private int rows;
	private int dpid;
	private int[] dpids;
	private String dpname;
	private String dpage;
	private String dpphone;
	private LMuser LMuser;
	private LMdistributionPerson lmp;
	public LMuser getLMuser() {
		return LMuser;
	}
	public void setLMuser(LMuser lMuser) {
		LMuser = lMuser;
	}
	public int[] getDpids() {
		return dpids;
	}
	public void setDpids(int[] dpids) {
		this.dpids = dpids;
	}
	public int getDpid() {
		return dpid;
	}
	public void setDpid(int dpid) {
		this.dpid = dpid;
	}
	public String getDpname() {
		return dpname;
	}
	public void setDpname(String dpname) {
		this.dpname = dpname;
	}
	public String getDpage() {
		return dpage;
	}
	public void setDpage(String dpage) {
		this.dpage = dpage;
	}
	public String getDpphone() {
		return dpphone;
	}
	public void setDpphone(String dpphone) {
		this.dpphone = dpphone;
	}
	public LMdistributionPerson getLmp() {
		return lmp;
	}
	public void setLmp(LMdistributionPerson lmp) {
		this.lmp = lmp;
	}
	public int getPage() {
		return page;
	}
	public void setPage(int page) {
		this.page = page;
	}
	public int getRows() {
		return rows;
	}
	public void setRows(int rows) {
		this.rows = rows;
	}
	@Override
	public void setServletResponse(HttpServletResponse response) {
		// TODO Auto-generated method stub
		this.response = response;
	}
	@Override
	public void setSession(Map<String, Object> session) {
		// TODO Auto-generated method stub
		this.session = session;
	}
	public String getStaff(){
		
			LMuser user = (LMuser) session.get("user");
			if(user!=null){
				int uid = user.getUid();
				JSONObject staffJO = NStaffservice.getStaffByUid(rows, page, uid);
				PrintWriterUtil.getPW(response).write(staffJO.toString());
				PrintWriterUtil.closePW(response);
				return null;
			}else{
				return ERROR;
			}
	}
	
	public String addStaff(){
		LMuser user = (LMuser) session.get("user");
		if(user!=null){
			int flag = NStaffservice.addStaff(dpname, dpage, dpphone, LMuser);
			if(flag == 0){
				PrintWriterUtil.getPW(response).write("0");
			}else if(flag == 1){
				PrintWriterUtil.getPW(response).write("1");
			}else{
				PrintWriterUtil.getPW(response).write("-1");
			}
			PrintWriterUtil.closePW(response);
			return null;
		}else{
			return ERROR;
		}
	}
	
	public String modifyStaff(){
		LMuser user = (LMuser) session.get("user");
		if(user!=null){
		LMdistributionPerson ll = new LMdistributionPerson();
		ll.setDpage(dpage);
		ll.setDpid(dpid);
		ll.setDpname(dpname);
		ll.setDpphone(dpphone);
		if(NStaffservice.modifyStaff(ll)){
			PrintWriterUtil.getPW(response).write("1");
		}else{
			PrintWriterUtil.getPW(response).write("-1");
		}
		PrintWriterUtil.closePW(response);	
		return null;
		}else{
			return ERROR;
		}
	}
	
	public void deleteStaffById(){
		System.out.println(dpids);
		if(NStaffservice.deleteStaffById(dpids)){
			PrintWriterUtil.getPW(response).write("1");
		}else{
			PrintWriterUtil.getPW(response).write("-1");
		}
		
	}
}




